// https://kns.cnki.net/kcms/detail/detail.aspx?dbcode=CAPJ&filename=YXZS2021112300N

const baseUrl = 'https://kns.cnki.net/kcms/detail/detail.aspx?'
const foreignUrl = 'https://schlr.cnki.net/en/Detail/index/'

/**
 * 根据tableName跳转
 * @param {String} tableName
 * @param {String} fileName
 */
export function openViewWithTableName(tableName, fileName) {
  const dbcode = tableName.substr(0, 4)
  window.open(baseUrl + 'dbcode=' + dbcode + '&filename=' + fileName)
}

/**
 * 根据来源数据库/DATA_TYPE + 语种 跳转
 * @param {String} dbOrtype 根据来源数据库/DATA_TYPE
 * @param {String} language 语种
 * @param {String} fileName 文件名
 */
export function openViewWithTypeAndLanguage(dbOrtype, language, fileName) {
  language = language.replace(';', '').replace(';;', '')
  const result = mapper[dbOrtype]
  const dbcode = getDbCode(result, language)
  window.open(baseUrl + 'dbcode=' + dbcode + '&filename=' + fileName)
}

/**
 * 打开知网作者
 * @param {*} authorCode 作者码
 */
export function openCnkiAuthor(authorCode) {
  window.open(`https://kns.cnki.net/kcms/detail/knetsearch.aspx?code=${authorCode}&sfield=au`)
}

function getDbCode(result, language) {
  for (let index = 0; index < result.key.length; index++) {
    const item = result.key[index]
    if (item === language) {
      return result.value
    }
  }
  return result.otherValue
}
/**
 * 外文跳转 根据tableName跳转
 * @param {String} tableName
 * @param {String} fileName
 */
export function openForeignViewWithTableName(tableName, fileName) {
  const dbcode = tableName.substr(0, 4)
  window.open(foreignUrl + dbcode + '/' + fileName)
}
const mapper = {
  期刊: {
    key: ['中文', 'Chinese', 'ZH'],
    value: 'CJFD',
    otherValue: 'GARJ'
  },
  报纸: {
    key: [],
    otherValue: 'CCND'
  },
  博士学位论文: {
    key: [],
    otherValue: 'CDFD'
  },
  硕士学位论文: {
    key: [],
    otherValue: 'CMFD'
  },
  案例: {
    key: [],
    otherValue: 'CLKC'
  },
  法律法规: {
    key: [],
    otherValue: 'CLKL'
  },
  学术会议: {
    key: ['中文', 'Chinese', 'ZH'],
    value: 'CPFD',
    otherValue: 'IPFD'
  },
  年鉴: {
    key: [],
    otherValue: 'CYFD'
  },
  Journal: {
    key: ['中文', 'Chinese', 'ZH'],
    value: 'CJFD',
    otherValue: 'GARJ'
  },
  Newspaper: {
    key: [],
    otherValue: 'CCND'
  },
  Doctoral_Thesis: {
    key: [],
    otherValue: 'CDFD'
  },
  Master_Thesis: {
    key: [],
    otherValue: 'CMFD'
  },
  Case: {
    key: [],
    otherValue: 'CLKC'
  },
  Laws: {
    key: [],
    otherValue: 'CLKL'
  },
  Conference: {
    key: ['中文', 'Chinese', 'ZH'],
    value: 'CPFD',
    otherValue: 'IPFD'
  },
  Yearbook: {
    key: [],
    otherValue: 'CYFD'
  }
}
